home *** CD-ROM | disk | FTP | other *** search
/ Champak 26 (Anniversary Edition) / Volume 26 [Anniversary Edition] - JOGO DISK .iso / DEPOSITO / REmovido / Esportes / LightningBreak.swf / scripts / frame_1 / DoAction_4.as < prev    next >
Text File  |  2006-06-13  |  12KB  |  403 lines

  1. _root.createEmptyMovieClip("g",100);
  2. _root.createEmptyMovieClip("gstep",101);
  3. CBall = function(id, mc)
  4. {
  5.    this.id = id;
  6.    this.value = id;
  7.    this.mc = mc;
  8.    this.x = mc._x;
  9.    this.y = mc._y;
  10.    this.radius = _root.ballSize / 2;
  11.    this.mc.engine = this;
  12.    this.mc.defaultWidth = this.mc._width;
  13.    this.mc.defaultHeight = this.mc._height;
  14.    this.glass = 0;
  15.    this.glassSmashed = 0;
  16.    this.reset();
  17. };
  18. CBall.prototype.reset = function()
  19. {
  20.    this.vx = 0;
  21.    this.vy = 0;
  22.    this.mc._x = this.x;
  23.    this.mc._y = this.y;
  24.    this.mc._xscale = 100;
  25.    this.mc._yscale = 100;
  26.    this.mc._alpha = 100;
  27.    this.mc._visible = true;
  28.    this.potted = -1;
  29.    this.glassSmashed = 0;
  30.    this.setGlass(0,1);
  31.    if(this.id == 1 && _root.lvl.colorSwap)
  32.    {
  33.       this.glass = 0;
  34.       this.colorSwap();
  35.    }
  36.    this.spot = 0;
  37.    this.mc.style.gotoAndStop(this.value + 1);
  38.    this.mc.style.label.gotoAndStop(this.value + 1);
  39.    this.mc.style.mc.gotoAndStop(this.glass + 1);
  40. };
  41. CBall.prototype.showLabel = function()
  42. {
  43.    this.mc.style.label._visible = 1;
  44. };
  45. CBall.prototype.hideLabel = function()
  46. {
  47.    this.mc.style.label._visible = 0;
  48. };
  49. CBall.prototype.initColorSwap = function()
  50. {
  51.    this.value = this.id;
  52.    this.mc.style.gotoAndStop(this.value + 1);
  53.    this.mc.style.label.gotoAndStop(this.value + 1);
  54. };
  55. CBall.prototype.colorSwap = function()
  56. {
  57.    if(this.id > 1)
  58.    {
  59.       this.value += 1;
  60.       if(this.value > 7)
  61.       {
  62.          this.value = 2;
  63.       }
  64.       this.mc.style.gotoAndStop(this.value + 1);
  65.       this.mc.style.label.gotoAndStop(this.value + 1);
  66.    }
  67. };
  68. CBall.prototype.setGlass = function(bool, force)
  69. {
  70.    if(this.potted != -1 && !force)
  71.    {
  72.       return undefined;
  73.    }
  74.    this.glass = bool;
  75.    this.mc.style.mc.gotoAndStop(this.glass + 1);
  76. };
  77. CBall.prototype.smashGlass = function()
  78. {
  79.    _root.snd.play("glass");
  80.    this.potted = 666;
  81.    this.spot = 1;
  82.    this.mc.style.mc.gotoAndStop("break");
  83.    this.glassSmashed = 1;
  84. };
  85. CBall.prototype.move = function()
  86. {
  87.    if(this.potted > -1)
  88.    {
  89.       if(this.mc._alpha == 0)
  90.       {
  91.          return undefined;
  92.       }
  93.       if(this.glassSmashed)
  94.       {
  95.          return undefined;
  96.       }
  97.       var hole = _root.table.holes[this.potted];
  98.       var a = _global.v2d.math.unify({x:hole.x - this.x,y:hole.y - this.y});
  99.       var d = Math.sqrt(Math.pow(this.tx - hole.x,2) + Math.pow(this.ty - hole.y,2)) * 0.08;
  100.       this.vx *= 0.75;
  101.       this.vy *= 0.75;
  102.       this.vx += a.x * d;
  103.       this.vy += a.y * d;
  104.       this.x += this.vx;
  105.       this.y += this.vy;
  106.       this.vz += 0.1;
  107.       this.mc._width -= this.vz;
  108.       this.mc._height -= this.vz;
  109.       this.mc._alpha *= Math.pow(0.95,_global.dt);
  110.       if(this.mc._alpha > 2)
  111.       {
  112.          _root.stepRollTotalMovement += 1;
  113.       }
  114.       else
  115.       {
  116.          this.mc._alpha = 0;
  117.          this.mc._visible = false;
  118.       }
  119.       this.redraw();
  120.       return undefined;
  121.    }
  122.    this.col = {pos:1,type:0,pvx:this.vx,pvy:this.vy};
  123.    if(this.id == 8 && _root.lvl.breakout)
  124.    {
  125.       while(!(this.vx + this.vy))
  126.       {
  127.          this.vx = 5;
  128.          this.vy = 0;
  129.          this.ax = 0;
  130.          this.ay = 0;
  131.       }
  132.       if(Math.random() < 0.05)
  133.       {
  134.          this.ax += (Math.random() - 0.5) * 0.5;
  135.          this.ay += (Math.random() - 0.5) * 0.5;
  136.       }
  137.       this.vx += this.ax;
  138.       this.vy += this.ay;
  139.       this.tv = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
  140.       this.vx = this.vx / this.tv * 6;
  141.       this.vy = this.vy / this.tv * 6;
  142.    }
  143.    this.tvx = this.vx * _global.dt;
  144.    this.tvy = this.vy * _global.dt;
  145.    this.tv = Math.sqrt(this.tvx * this.tvx + this.tvy * this.tvy);
  146.    if(this.id < 8)
  147.    {
  148.       _root.stepRollTotalMovement += this.tv;
  149.    }
  150.    this.tx = this.x + this.tvx;
  151.    this.ty = this.y + this.tvy;
  152.    if(this.glassSmashed == 1)
  153.    {
  154.       _root.stepRollTotalMovement += 2;
  155.    }
  156.    if(!(this.vx == 0 && this.vy == 0))
  157.    {
  158.       var colPosTemp;
  159.       for(var i in _root.table.hwalls)
  160.       {
  161.          var wall = _root.table.hwalls[i];
  162.          if(Math.min(this.y,this.ty) - this.radius <= wall.y0 && Math.max(this.y,this.ty) + this.radius >= wall.y0)
  163.          {
  164.             if(this.ty * wall.dir <= wall.y0 * wall.dir + this.radius && this.y * wall.dir >= wall.y0 * wall.dir + this.radius - 0.000001)
  165.             {
  166.                var hitX = this.x + (wall.y0 + this.radius * wall.dir - this.y) / this.tvy * this.tvx;
  167.                if(hitX >= wall.x0 && hitX <= wall.x1)
  168.                {
  169.                   colPosTemp = 1 - (this.ty - (wall.y0 + this.radius * wall.dir)) / this.tvy;
  170.                   if(colPosTemp < this.col.pos)
  171.                   {
  172.                      this.col.type = 1;
  173.                      this.col.pos = colPosTemp;
  174.                      this.col.pvx = this.tvx / _global.dt;
  175.                      this.col.pvy = Math.abs(this.tvy * _root.table.flex) * wall.dir / _global.dt;
  176.                   }
  177.                }
  178.             }
  179.             this.pointCollision({x:wall.x0,y:wall.y0});
  180.             this.pointCollision({x:wall.x1,y:wall.y1});
  181.          }
  182.       }
  183.       for(var i in _root.table.vwalls)
  184.       {
  185.          var wall = _root.table.vwalls[i];
  186.          if(Math.min(this.x,this.tx) - this.radius <= wall.x0 && Math.max(this.x,this.tx) + this.radius >= wall.x0)
  187.          {
  188.             if(this.tx * wall.dir <= wall.x0 * wall.dir + this.radius && this.x * wall.dir >= wall.x0 * wall.dir + this.radius - 0.000001)
  189.             {
  190.                var hitY = this.y + (wall.x0 + this.radius * wall.dir - this.x) / this.tvx * this.tvy;
  191.                if(hitY >= wall.y0 && hitY <= wall.y1)
  192.                {
  193.                   colPosTemp = 1 - (this.tx - (wall.x0 + this.radius * wall.dir)) / this.tvx;
  194.                   if(colPosTemp < this.col.pos)
  195.                   {
  196.                      this.col.type = 1;
  197.                      this.col.pos = colPosTemp;
  198.                      this.col.pvy = this.tvy / _global.dt;
  199.                      this.col.pvx = Math.abs(this.tvx * _root.table.flex) * wall.dir / _global.dt;
  200.                   }
  201.                }
  202.             }
  203.             this.pointCollision({x:wall.x0,y:wall.y0});
  204.             this.pointCollision({x:wall.x1,y:wall.y1});
  205.          }
  206.       }
  207.       var i = 0;
  208.       while(i < _root.ballsAmount + _root.breakout)
  209.       {
  210.          if(i != this.id)
  211.          {
  212.             this.sphereCollision(_root.balls[i]);
  213.          }
  214.          i++;
  215.       }
  216.       var i = 0;
  217.       while(i < 6)
  218.       {
  219.          if(_root.lids[i].closed)
  220.          {
  221.             this.pointCollision(_root.lidPoints[i]);
  222.             this.pointCollision(_root.lidPoints2[i]);
  223.          }
  224.          i++;
  225.       }
  226.       if(this.col.type == 1)
  227.       {
  228.          this.tx = this.x + this.tvx * this.col.pos;
  229.          this.ty = this.y + this.tvy * this.col.pos;
  230.          _root.snd.playEvent("reflect",this.tv * 0.05);
  231.       }
  232.       this.x = this.tx;
  233.       this.y = this.ty;
  234.       this.vx = this.col.pvx;
  235.       this.vy = this.col.pvy;
  236.       if(this.col.type == 2)
  237.       {
  238.          this.col.q.vx = this.col.qvx;
  239.          this.col.q.vy = this.col.qvy;
  240.       }
  241.       var f = 1 - 0.09 * (1 - Math.min(Math.abs(this.vx) + Math.abs(this.vy),3) * 0.33);
  242.       f = Math.pow(f * _root.lvl.friction,_global.dt);
  243.       this.vx *= f;
  244.       this.vy *= f;
  245.       this.redraw();
  246.    }
  247.    var hole;
  248.    var i = 0;
  249.    while(i < 6)
  250.    {
  251.       if(!_root.lids[i].closed)
  252.       {
  253.          hole = _root.table.holes[i];
  254.          if(Math.abs(this.tx - hole.x) <= hole.radius && Math.abs(this.ty - hole.y) <= hole.radius)
  255.          {
  256.             var d = Math.sqrt(Math.pow(this.tx - hole.x,2) + Math.pow(this.ty - hole.y,2));
  257.             if(d < hole.radius)
  258.             {
  259.                if(this.id != 8)
  260.                {
  261.                   _root.pot(this.value,this.id);
  262.                }
  263.                else
  264.                {
  265.                   this.spot = 1;
  266.                }
  267.                _root["h" + i].flash(this.value);
  268.                this.potted = i;
  269.                this.vz = 0;
  270.             }
  271.          }
  272.       }
  273.       i++;
  274.    }
  275. };
  276. CBall.prototype.sphereCollision = function(ball)
  277. {
  278.    if(ball.potted != -1)
  279.    {
  280.       return undefined;
  281.    }
  282.    var a = ball.x - this.x;
  283.    var b = ball.y - this.y;
  284.    var c = Math.pow(this.tvx,2) + Math.pow(this.tvy,2);
  285.    var d = (this.tvy * a - this.tvx * b) / c * this.tv * -1;
  286.    var j = Math.sqrt(Math.pow(this.radius + ball.radius,2) - d * d);
  287.    if(!isNaN(j))
  288.    {
  289.       var p = (this.tvx * a + this.tvy * b) / c;
  290.       p *= 1 - j / (p * this.tv);
  291.       if(p < 0 && p > -0.000001)
  292.       {
  293.          p = 0;
  294.       }
  295.       if(p >= 0 && p < this.col.pos)
  296.       {
  297.          if(this.glass || ball.glass)
  298.          {
  299.             if(this.glass)
  300.             {
  301.                this.smashGlass();
  302.             }
  303.             if(ball.glass)
  304.             {
  305.                ball.smashGlass();
  306.             }
  307.          }
  308.          var qax;
  309.          var qay;
  310.          var pvx;
  311.          var pvy;
  312.          var colvx;
  313.          var colvy;
  314.          var pv;
  315.          var qv;
  316.          this.col.pos = p;
  317.          this.col.type = 2;
  318.          this.tx = this.x + this.tvx * p;
  319.          this.ty = this.y + this.tvy * p;
  320.          qax = ball.x - this.tx;
  321.          qay = ball.y - this.ty;
  322.          pvx = qay * -1;
  323.          pvy = qax;
  324.          colvx = this.vx - ball.vx;
  325.          colvy = this.vy - ball.vy;
  326.          var colv = Math.sqrt(colvx * colvx + colvy * colvy);
  327.          _root.snd.playEvent("ballcollide",colv / 50);
  328.          pv = (colvx * qay - colvy * qax) / (pvx * qay - pvy * qax);
  329.          this.col.q = ball;
  330.          qv = (colvy - pv * qax) / qay;
  331.          if(qv eq "NaN")
  332.          {
  333.             this.col.qvx = this.vx;
  334.             this.col.qvy = this.vy;
  335.          }
  336.          else
  337.          {
  338.             qax *= qv;
  339.             qay *= qv;
  340.             this.col.qvx = ball.vx + qax;
  341.             this.col.qvy = ball.vy + qay;
  342.          }
  343.          pvx *= pv;
  344.          pvy *= pv;
  345.          this.col.pvx = pvx;
  346.          this.col.pvy = pvy;
  347.          _root.collision(this,ball);
  348.       }
  349.    }
  350. };
  351. CBall.prototype.pointCollision = function(point)
  352. {
  353.    var a = point.x - this.x;
  354.    var b = point.y - this.y;
  355.    var c = Math.pow(this.tvx,2) + Math.pow(this.tvy,2);
  356.    var d = (this.tvy * a - this.tvx * b) / c * this.tv * -1;
  357.    var j = Math.sqrt(this.radius * this.radius - d * d);
  358.    if(!isNaN(j))
  359.    {
  360.       var p = (this.tvx * a + this.tvy * b) / c;
  361.       p *= 1 - j / (p * this.tv);
  362.       if(p < 0 && p > -0.000001)
  363.       {
  364.          p = 0;
  365.       }
  366.       if(p >= 0 && p < this.col.pos)
  367.       {
  368.          this.col.pos = p;
  369.          this.col.type = 3;
  370.          this.tx = this.x + this.tvx * p;
  371.          this.ty = this.y + this.tvy * p;
  372.          var x1 = point.x - this.x;
  373.          var y1 = point.y - this.y;
  374.          var n1 = ((x1 * x1 + y1 * y1) / (this.tvx * x1 + this.tvy * y1) * this.tvy - y1) / x1;
  375.          var pvx = y1 * n1 * -1 - x1;
  376.          var pvy = x1 * n1 - y1;
  377.          var pv = this.tv * _root.table.flex / _global.dt / Math.sqrt(pvx * pvx + pvy * pvy);
  378.          this.col.pvx = pvx * pv;
  379.          this.col.pvy = Pvy * pv;
  380.       }
  381.    }
  382. };
  383. CBall.prototype.redraw = function()
  384. {
  385.    this.mc._x = this.x;
  386.    this.mc._y = this.y;
  387. };
  388. CBall.prototype.setSpeed = function(vx, vy)
  389. {
  390.    this.vx = vx;
  391.    this.vy = vy;
  392. };
  393. CBall.prototype.setPos = function(x, y)
  394. {
  395.    this.x = x;
  396.    this.y = y;
  397.    this.redraw();
  398. };
  399. CBall.prototype.toString = function()
  400. {
  401.    return "[CBall mc:" + this.mc + ", x:" + this.x + ", y:" + this.y + ", vx:" + this.vx + ", vy:" + this.vy + "]";
  402. };
  403.